package cn.com.nes.site.service.templatedesign.impl;


import cn.com.nes.common.util.Globals;
import cn.com.nes.common.util.OssUtil;
import cn.com.nes.common.util.ParameterUtil;
import cn.com.nes.mybatis.agent.templatedesign.mapper.HHousemodelMapper;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.templatedesign.HHousemodelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类
 *
 * @author
 * @since 2023-09-26
 */
@Service
public class HHousemodelServiceImpl implements HHousemodelService {
    @Resource
    private HHousemodelMapper housemodelMapper;

    /**
     * 添加模板
     * 已联调
     *
     * @param req
     * @return
     */
    @Override
    public PageData addHouseModel(DataReq req) {
        String showpicfilename = ParameterUtil.getString(req, "showpicfilename");
        String showpicpath = showpicfilename;
        String detailpicfilename = ParameterUtil.getString(req, "detailpicfilename");
        String detailpicpath = detailpicfilename;
        String CADfiletxt = ParameterUtil.getString(req, "CADfiletxt");
        String CADfilename = ParameterUtil.getString(req, "CADfilename");
        String CADfile = CADfilename;
        String modeltype = ParameterUtil.getString(req, "modeltype");
        String modeltypename = null;
        if (modeltype.equalsIgnoreCase("01")) {
            modeltypename = "一室一厅";
        } else if (modeltype.equalsIgnoreCase("02")) {
            modeltypename = "两室一厅";
        } else if (modeltype.equalsIgnoreCase("03")) {
            modeltypename = "两室两厅";
        } else if (modeltype.equalsIgnoreCase("04")) {
            modeltypename = "三室一厅";
        } else if (modeltype.equalsIgnoreCase("05")) {
            modeltypename = "三室两厅";
        }
        String modelname = ParameterUtil.getString(req, "modelname");
        String createUid = ParameterUtil.getString(req, "createUid");
        String introduction = ParameterUtil.getString(req, "introduction");
        String createTime = ParameterUtil.getString(req, "createTime");
        String createName = ParameterUtil.getString(req, "createName");
        housemodelMapper.addHouseModel(modeltype, modelname, introduction, showpicpath, showpicfilename, detailpicpath, detailpicfilename, CADfiletxt, CADfile, CADfilename, createUid, createTime, createName, modeltypename);
        return PageData.ok();

    }


    /**
     * 编辑模板
     * 已联调
     *
     * @param req
     * @return
     */
    @Override
    public int updateHouseModel(DataReq req) {

        int housemodelid = ParameterUtil.getInteger(req, "housemodelid");
        String showpicfilename = ParameterUtil.getString(req, "showpicfilename");
        String showpicpath = showpicfilename;
        String detailpicfilename = ParameterUtil.getString(req, "detailpicfilename");
        String detailpicpath = detailpicfilename;
        String CADfiletxt = ParameterUtil.getString(req, "CADfiletxt");
        String CADfilename = ParameterUtil.getString(req, "CADfilename");
        String CADfile = CADfilename;
        String modeltype = ParameterUtil.getString(req, "modeltype");
        String modeltypename = null;
        if (modeltype.equalsIgnoreCase("01")) {
            modeltypename = "一室一厅";
        } else if (modeltype.equalsIgnoreCase("02")) {
            modeltypename = "两室一厅";
        } else if (modeltype.equalsIgnoreCase("03")) {
            modeltypename = "两室两厅";
        } else if (modeltype.equalsIgnoreCase("04")) {
            modeltypename = "三室一厅";
        } else if (modeltype.equalsIgnoreCase("05")) {
            modeltypename = "三室两厅";
        }
        String modelname = ParameterUtil.getString(req, "modelname");
        String createName = ParameterUtil.getString(req, "createName");
        String introduction = ParameterUtil.getString(req, "introduction");
        String modifyUid = ParameterUtil.getString(req, "modifyUid");
        int count = housemodelMapper.updateByHouseModelId(housemodelid, modeltype, modelname, introduction, showpicpath, showpicfilename, detailpicpath, detailpicfilename, CADfiletxt, CADfile, CADfilename, modeltypename, modifyUid, createName);
        return count;
    }

    /**
     * 删除模板
     * 已联调
     *
     * @param req
     * @return
     */
    @Override
    public int deleteHouseModel(DataReq req) {
        int housemodelid = ParameterUtil.getInteger(req, "housemodelid");
        int count = housemodelMapper.updateIsDeleteByHouseModelId(housemodelid);
        return count;
    }

    /**
     * 用于统计页数的查询
     * 已联调
     *
     * @param req
     * @return
     */
    @Override
    public int selectALLlist(DataReq req) {

        int Count = housemodelMapper.selectALLlist();
        return Count;
    }

    /**
     * 查询模板列表
     * 已联调
     *
     * @param req
     * @return
     */
    @Override
    public List<Map<String, Object>> selectByModelNameorType(DataReq req) {
        int pageid = ParameterUtil.getInteger(req, "pageid");
        int pagesize = ParameterUtil.getInteger(req, "pagesize");
        String modelname = ParameterUtil.getString(req, "modelname");
        String modeltype = ParameterUtil.getString(req, "modeltype");
        Integer housemodelid = ParameterUtil.getInteger(req, "housemodelid");
        //计算页数对应的起始数据位置 (pageid*pagesize-pagesize)
        int offset = pageid * pagesize - pagesize;
        String strshowpicpath = null;
        String strdetailpicpath = null;
        String strCADfile = null;
        String createTime = null;
        String modifyTime = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        List<Map<String, Object>> list = housemodelMapper.selectByModelNameorType(housemodelid, modeltype, modelname, offset, pagesize);
        int listSize = list.size();
//        返回ossurl
        String ossShowPicPath = null;
        String ossDetailPicPath = null;
        String ossCadFilePath = null;
        for (int i = 0; i < listSize; i++) {
//      给前端返回showpic，detailpic，cadfile各自对应的ossurlList
            if (list.get(i).get("showpicfilename") != null) {
                strshowpicpath = (String) list.get(i).get("showpicfilename");
                List<String> strshowpicpathList = new ArrayList<>();
                String[] strshowpicpathArr = strshowpicpath.split(";");
                for (int i1 = 0; i1 < strshowpicpathArr.length; i1++) {
                    ossShowPicPath = OssUtil.getImgSignURL(strshowpicpathArr[i1], 0);
                    strshowpicpathList.add(ossShowPicPath);
                }
                list.get(i).put("showpicpath", strshowpicpathList);
            }
            if (list.get(i).get("detailpicfilename") != null) {
                strdetailpicpath = (String) list.get(i).get("detailpicfilename");
                String[] strdetailpicpathArr = strdetailpicpath.split(";");
                List<String> strdetailpicpathList = new ArrayList<>();
                for (int i1 = 0; i1 < strdetailpicpathArr.length; i1++) {
                    ossDetailPicPath = OssUtil.getImgSignURL(strdetailpicpathArr[i1], 0);
                    strdetailpicpathList.add(ossDetailPicPath);
                }
                list.get(i).put("detailpicpath", strdetailpicpathList);

            }
            if (list.get(i).get("CADfilename") != null) {
                strCADfile = (String) list.get(i).get("CADfilename");
                String[] CADfileArr = strCADfile.split(";");
                List<String> strCADfileList = new ArrayList<>();
                for (int i1 = 0; i1 < CADfileArr.length; i1++) {
                    ossCadFilePath = OssUtil.getFileSignURL(CADfileArr[i1], 0);
                    strCADfileList.add(ossCadFilePath);
                }
                list.get(i).put("CADfile", strCADfileList);
            }
            if (list.get(i).get("createTime") != null) {
                createTime = simpleDateFormat.format(list.get(i).get("createTime"));
                list.get(i).put("createTime", createTime);
            }
            if (list.get(i).get("modifyTime") != null) {
                modifyTime = simpleDateFormat.format(list.get(i).get("modifyTime"));
                list.get(i).put("modifyTime", modifyTime);
            }
        }
        return list;
    }

    /**
     * 防止添加重名模板
     *
     * @param req
     * @return
     */
    @Override
    public List<Map<String, Object>> selectByModelName(DataReq req) {
        String modelname = ParameterUtil.getString(req, "modelname");
        List<Map<String, Object>> list = housemodelMapper.selectByModelName(modelname);
        return list;
    }


}
